<template>
<div class="ui form marginTop10">
    <tFields @onsubmit="onsubmit" @onchange="onchange" :data="[addData[0],addData[1]]" :nosubmit="nosubmit"></tFields>
    <mybind :field='whField' @setVo='setSpid' v-show='oldOrNew'></mybind>
    <tFields @onsubmit="onsubmit" @onchange="onchange" :data="fieldData" v-show='!oldOrNew' :nosubmit="nosubmit"></tFields>
    <div class="center-box">
        <button class="ui primary button" @click.prevent.stop="sure">确认</button>
        <button class="ui button" @click.prevent.stop="hideMypop">取消</button>
    </div>
</div>
</template>
<script>
import tool from 'src/util/tool';
import util from '_util/index';
import logic from '_logic';

import mybind from '../../field/bind'
import tFields from 'components/tunit/tFields'

export default {
    data() {
        return {
            val: null,
            nosubmit: true,
            whField: {
                title: '',
                data_type: 0,
                name: 'id',
                bind_name_field: 'name',
                bindModel: 44,
                fval: '',
            },
            pageData: {
                oldOrNew: '已有仓库',
                name: '',
                position: '',
                decs: '',
                ol: '有效',
                id: ''
            },
            addData: [{
                    label: "是否优惠规格",
                    show_type: "select",
                    name: "is_discounts",
                    element: ['是', '否'],
                    value: '否'
                },
                {
                    label: '',
                    show_type: 'radio',
                    name: 'oldOrNew',
                    element: ['已有仓库', '新增仓库'],
                    value: '已有仓库'
                }, {
                    label: "仓库名称",
                    show_type: "input",
                    name: "name",
                    value: ''
                },
                {
                    label: "仓库地址",
                    show_type: "input",
                    name: "position",
                    value: ''
                },
                {
                    label: '描述',
                    show_type: "textarea",
                    name: 'decs',
                    value: '',
                }
            ],
        }
    },
    watch: {
		'option.ids':function(val,oldval){

		}
	},
    methods: {
        setSpid(sp, id, name) {
            this.pageData.id = id
        },
        hideMypop() {
            this.$store.state.global.eventVue.$emit('hideMypop')
        },
        onchange({
            name,
            value
        }) {
            this.pageData[name] = value;
        },
        add_pro_wh(id) {
			let arr = Array.from(this.option.ids)
            let param = {
                product_ids: arr.toString(),
                warehouse_id: this.pageData.id,
                is_discounts: this.isDiscounts,
            };
            util.http.SR(this, '/index/Product/addWarehouseProduct', param, '添加成功', (rs) => {

//            this.hideMypop();
//			this.$router.push(`/page/list?ctl=WarehouseTopProduct&warehouse_id=${this.pageData.id}`);
			window.location.href = `/page/list?ctl=WarehouseTopProduct&warehouse_id=${this.pageData.id}`
            });


        },
        sure() {
            if (this.isDiscounts > -1) {
                // 添加商品到仓库
                if (this.oldOrNew) {
                    //现有仓库
                    this.add_pro_wh()
                } else {
                    //新增仓库
                    util.http.SR(this, '/index/Warehouse/insert', {
                        name: this.pageData.name,
                        decs: this.pageData.decs,
                        ol: this.pageData.ol,
                        position: this.pageData.position
                    }, '新增仓库成功', (rs) => {
                        this.pageData.id = rs.id;
                        this.add_pro_wh();
                    });
                }
            } else {
                util.showMsg(this, 'error', '请选择是否优惠');
            }

        },
        oncancel() {},
        onsubmit() {},
    },
    computed: {
        addDataIndexMap() {
            let data = {};
            this.addData.forEach((item, index) => {
                data[item.name] = index
            })
            return data
        },
        isDiscounts() {
            return this.pageData.is_discounts === '是' ? 1 : 0
        },
        fieldData() {
            return this.addData.splice(2)
        },
        oldOrNew() {
            return this.pageData.oldOrNew == "已有仓库"
        }
    },
    props: ['option'],
    components: {
        tFields,
        mybind
    },

}
</script>
<style rel="stylesheet/less" scoped lang="less">
@import "../../../common/style/mixin";
.center-box {
    text-align: center;
    width: 100%;
}
</style>
